<?php
/**
 * @user:andy
 * @date:2021/7/3
 * @company:http://www.sjdphp.com
 * @desc:
 */

namespace app\admin\business;


use app\common\server\Json;
use app\common\server\SystemLog;
use think\facade\Request;

class BusBase
{
    /** 通用化获取多条数据
     * @param $term
     * @param string $field
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return \think\response\Json
     * @throws \ReflectionException
     */
    public static function getLists($term,$field = '',$page = 0,$limit = 0,$order = ''){
        $model = self::getModelExample();

        ## 查询操作
        try{
            $lists = $model->where($term)->field($field)->page($page,$limit)->order($order)->select();
            $count = $model->where($term)->count();
        }catch(\Exception $e){
            SystemLog::insertLog($e->getMessage());
            return Json::error("系统繁忙,请稍后重试");
        }

        if(!empty($lists) && !empty($count)){
            return json(array("code" => 0,'msg' => '获取成功','count' => $count,'data' => $lists));
        }else{
            return Json::error('空数据');
        }
    }


    /** 通用化获取多条数据的数组
     * @param $term
     * @param string $field
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return mixed
     * @throws \ReflectionException
     */
    public static function getArrayLists($term,$field = '',$page = 0,$limit = 0,$order = ''){
        $model = self::getModelExample();

        try{
            $lists = $model->where($term)->field($field)->page($page,$limit)->order($order)->select();
        }catch(\Exception $e){
            SystemLog::insertLog($e->getMessage());
        }

        return empty($lists) ? '' : $lists->toArray();
    }

    /** 获取单条数据
     * @param $term
     * @param string $field
     * @return mixed
     * @throws \ReflectionException
     */
    public static function getInfo($term,$field = ''){
        $model = self::getModelExample();

        try{
            $info = $model->where($term)->field($field)->find();
        }catch(\Exception $e){
            SystemLog::insertLog($e->getMessage());
        }

        return empty($info) ? $info : $info->toArray();
    }


    /** 通用化新增与更新操作
     * @param $param
     * @param int $type
     * @return \think\response\Json
     * @throws \ReflectionException
     */
    public static function insertOrUpdateAndDel($param,$type = 1){
        $model = self::getModelExample();

        ## 判断是否存在上传后的数据
        if(isset($param["file"])){
            unset($param["file"]);
        }

        ## 判断操作类型
        $str = '';
        switch ($type){
            case 1:
                $res = $model::create($param);
                $str = '新增';
                break;
            case 2:
                $res = $model::update($param);
                $str = '更新';
                break;
            default:
                $res = $model::update($param);
                $str = '删除';
                break;
        }

        if($res){
            return Json::success($str . '成功');
        }else{
            return Json::error($str . "失败");
        }

    }


    /** 反射机制获取模型实例
     * @return object
     * @throws \ReflectionException
     */
    protected static function getModelExample($name = ''){
        if(!empty($name)){
            $modelPath = "app\common\model\\" . $name;
        }else{
            $modelPath = "app\common\model\\" . Request::controller();
        }


        return (new \ReflectionClass($modelPath))->newInstance();
    }
}